package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Resource
    private DishMapper dishMapper;
    @Resource
    private DishFlavorMapper dishFlavorMapper;
    @Resource
    private SetMealDishMapper setMealDishMapper;

    /**
     * 菜品新增
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void add(DishDTO dishDTO) {

//        1.dishDTO里面菜品赋值给dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
//        2.菜品插入数据库 获取自增长主键值
        dishMapper.insert(dish);

//        3.遍历口味列表，设置每个口味对象的逻辑外键dishId
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        if (dishFlavorList == null || dishFlavorList.isEmpty()) {
            throw new BaseException("请输入口味数据");
        }
        dishFlavorList.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());

        });

//        4.菜品口味插入数据库
        dishFlavorMapper.batchInserts(dishFlavorList);
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
//        设置分页参数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
//        执行数据库操作
        List<DishVO> dishVOList = dishMapper.findByCondition(dishPageQueryDTO);
        Page<DishVO> page = (Page<DishVO>) dishVOList;
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 修改菜品接口
     *
     * @param dishDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

        dishFlavorMapper.deleteByDIshId(dish.getId());
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        if (dishFlavorList == null || dishFlavorList.isEmpty()) {
            throw new BaseException("口味不能为空");
        }
        dishFlavorList.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishFlavorMapper.batchInserts(dishFlavorList);
    }

    /**
     * 根据id查询菜品接口
     *
     * @param id
     * @return
     */
    @Override
    public DishVO findById(Long id) {
        Dish dish = dishMapper.findById(id);
        List<DishFlavor> dishFlavorList = dishFlavorMapper.findByIDishId(dish.getId());
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 删除菜品接口
     *
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
//        根据ids查询是否存在起售的菜品
        Integer count = dishMapper.findStatusCount(ids, StatusConstant.ENABLE);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

//        根据ids查询是否存在套餐菜品数据
        count = setMealDishMapper.findCountByDishIds(ids);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

//        删除菜品数据
        dishMapper.batchDelete(ids);
//        删除菜品对应口味列表数据
        dishFlavorMapper.batchDelete(ids);

    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.findByIDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
}
